﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// Authors Patrick Boos, Michael Gfeller, Silvan Gehrig
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion


#region Usings

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Surface.Presentation.Controls;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Config.ElementManagement;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.View.WinTouch.UserControls.View;
using ScrumTable.UI.View.WinTouch.Util;
using ScrumTable.UI.ViewModel;
using ScrumTable.UI.ViewModel.Controller;

#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls
{
    /// <summary>
    /// Shows a User Story as a Stack: Shows the User-Story and all childs (tasks)
    /// 
    /// </summary>
    public partial class BoardUserStoryControl 
    {


        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
  
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// DependencyProperty for UserStorie.
        /// </summary>
        public static readonly DependencyProperty UserStorieProperty =
            DependencyProperty.Register("BoardUserStory",
                                        typeof(UserStoryViewModel),
                                        typeof(BoardUserStoryControl),
                                        new UIPropertyMetadata(null));

        /// <summary>
        /// The Control shows all childs (Tasks) of this UserStory 
        /// </summary>
        /// <value></value>
        public UserStoryViewModel BoardUserStory
        {
            get
            {
                return (UserStoryViewModel)GetValue(UserStorieProperty);
            }
            set
            {
                SetValue(UserStorieProperty, value);

                CreateDynamicPart();
            }
        }

        public IEnumerable<ScrumState> States
        {
            get
            {
                return ElementManager.GetStates(ElementManager.GetTargetConfig(NavigationTarget.DailyScrumBoard.ToString()));
            }
        }
        #endregion


        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------


        /// <summary>
        /// Initializes a new instance of the <see cref="StackedUserStoryControl"/> class.
        /// </summary>
        public BoardUserStoryControl()
        {
            InitializeComponent();
            
            DataContextChanged += OnBoardUserStoryControlDataContextChanged;
            FirstColumn.Width = new GridLength(StackedDailyScrumStoryBoard.LengthFirstColumn);
        }

        
      
        #endregion
        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
           DataContextChanged -= OnBoardUserStoryControlDataContextChanged;
           base.ReleaseManagedResources();
        }


        private void CreateDynamicPart()
        {
            
            int i = 1;
            foreach (var state in States)
            {
                Grid.ColumnDefinitions.Add(new ColumnDefinition());
                StackedUserStoryControl control = new StackedUserStoryControl() {NumbersOfOtherControls = States.Count(), Margin = new Thickness(5, 0, 5, 0), HorizontalAlignment = HorizontalAlignment.Stretch, HorizontalContentAlignment = HorizontalAlignment.Stretch, Background = Resources["ItemBrush"] as Brush };
                
                Grid.SetColumn(control, i);

                BindingUtil.BindElementPath(control, StackedUserStoryControl.IsExpanedProperty,"BoardUserStory.IsExpaned", "BoardControl");

                control.StackedUserStory = BoardUserStory;
                control.StoryState = state;
                Grid.Children.Add(control);

                i++;
            }
        }
        #endregion

   
     
       
        #region events
        private void OnBoardUserStoryControlDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (DataContext is UserStoryViewModel)
            {
                BoardUserStory = (UserStoryViewModel) DataContext;
            }

        }


        private void OnAddBugClick(object sender, RoutedEventArgs e)
        {
            if (BoardUserStory != null)
            {
                var bug = BoardUserStory.Original.Bugs.AddNew();
                var taskBack = new BugControlBack() { DataContext = new BugViewModel(bug) };
                ((AppScreen)Application.Current.MainWindow).AddScatteredItem(taskBack, ((SurfaceButton)sender).PointToScreen(new Point(0, 0)), true);
            }
        }

        private void OnAddTaskClick(object sender, RoutedEventArgs e)
        {
            if (BoardUserStory!=null)
            {
                var task = BoardUserStory.Original.Tasks.AddNew();
                var taskBack = new TaskControlBack() { DataContext = new TaskViewModel(task) };
                ((AppScreen)Application.Current.MainWindow).AddScatteredItem(taskBack, ((Button)sender).PointToScreen(new Point(0, 0)), true);
            }
        }
      
        private void OnTouchDown(object sender, TouchEventArgs e)
        {
            e.Handled = true;
        }

        private void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        #endregion

       

    }
}
